<template>
	<view class="content">
		<view class="rich-text">
			<rich-text :nodes="html"></rich-text>
		</view>
		<view class="btn">
			<view class="btn-wrap">
				<!-- #ifdef MP-WEIXIN -->
				<button type="primary" open-type="getPhoneNumber" @getphonenumber="handlePhone">确定同意协议</button>
				<!-- #endif -->
				<!-- #ifdef MP-ALIPAY -->
				<button type="primary" v-if="alipayParam=='1'" open-type="getPhoneNumber"
					@getphonenumber="handlePhone">确定同意协议</button>
				<button type="primary" v-else @click="handleKnowClick">我已知晓</button>
				<!-- #endif -->
			</view>
			<view class="bottom-safe-area"></view>
		</view>
		<view class="bottom-safe-area"></view>
		<!--单悬浮客服-->
		<common-drag />
	</view>
</template>
<script>
	import {
		BASE_URL
	} from '../../env.js';
	export default {
		data() {
			return {
				html: '',
				code: null,
				alipayParam: '1',
			}
		},
		onLoad(options) {
			this._getCode();
			this.getHtmlRequest();
		},
		methods: {
			getHtmlRequest() {
				this.$u.api.getUserAgreement().then(res => {

					//*微信
					// #ifdef	MP-WEIXIN
					this.html = res.data.agreement;
					// #endif


					// #ifdef	MP-ALIPAY
					this.html = this.$parseHtml(res.data.agreement);
					// #endif
				});
			},
			//get login code
			_getCode() {
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: function(res) {
						_this.code = res.code
					}
				});
			},
			handlePhone(e) {


				let uid = uni.getStorageSync('uid') || '';
				if (e.detail.errMsg == "getPhoneNumber:ok") {

					let params = {
						encryptedData: e.detail.encryptedData,
						iv: e.detail.iv,
						code: this.code
					};

					this.$u.api.phoneAuthorization({
						remind: '1',
						...params,
						uid
					}).then(res => {

						if (res.result_code == "361") {
							//成功了，完结
							this.$utils.toastCallback(res.msg, () => {
								uni.setStorageSync('userPhone', res.phone);
								if (res.userid_locked) {
									uni.setStorageSync('userId', res.userid_locked);
								};
								this.$utils.back();
							})
						} else if (res.result_code == "2") {
							//弹窗提示用户需要合并微信支付宝账户了
							this.$utils.modal({
								title: '温馨提示',
								content: res.msg,
								callback: (bool) => {
									if (bool) {

										this.$u.api.phoneAuthorization({
											remind: '2',
											...params,
											uid
										}).then(ret => {
											if (ret.result_code == "361") {
												//成功了
												this.$utils.toastCallback(ret.msg, () => {
													uni.setStorageSync('userPhone', ret
														.phone);
													if (ret.userid_locked) {
														uni.setStorageSync('userId',
															ret.userid_locked);
													};
													this.$utils.back();
												})
											} else {
												//失败
												this.$utils.modal({
													title: '温馨提示',
													content: ret.msg,
													showCancel: false
												});
											};
										});
									};
								}
							});
						} else {
							//失败
							this.$utils.modal({
								title: '温馨提示',
								content: res.msg,
								showCancel: false
							});
						};
					});

				} else {
					this.$utils.toast('您取消了授权');
				};
			},
			//临时支付宝代码
			handleKnowClick() {
				this.$utils.back();
			}
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		padding: 24rpx;
		padding-bottom: 180rpx;
	}

	.btn {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		z-index: 100;
		padding-top: 20rpx;
		padding-bottom: 20rpx;
	}

	.btn-wrap {
		padding: 0 20rpx;
	}
</style>